package com.dtf.admin.role.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dtf.admin.base.JqGridFilter;
import com.dtf.admin.role.form.SystemRoleForm;
import com.dtf.admin.role.vo.SystemRole;
import com.dtf.core.common.service.BaseService;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.vo.Pagination;


/**
 * 访问dao层的方式目前有三种
 * 1：继承baseService，可用super获取到Spring 的 jdbcTemplt工具类
 * 2:从super获取BaseDaoSupport获取到super.getDaoSupport().getNamedParameterJdbcTemplate()提供Spring内部封装的工具类
 * 3：从super获取BaseDaoSupport，系统内部自己对两个工具类进行的二次封装，主要用于分页查询等功能
 * @author Administrator
 *
 */
@Service
public class SystemRoleService extends BaseService{
	
	private static Logger log = Logger.getLogger(SystemRoleService.class);
	
	public Map queryRoleList(SystemRoleForm form){
		
		JqGridFilter filter = form.getJqGridFilter();
		String sql = "select * from system_role ";
		Map filterMap = filter.getReq();
		String where = Const.getStrValue(filterMap,"sql");
		List params = (List) filterMap.get("param");
		if(!StringUtils.isEmpty(where)){
			sql += "where " + where;
		}
		Pagination pagination = super.getDaoSupport().queryListMapForPage(sql, params, form.getPageSize(), form.getPage());
		
		Map map = new HashMap();
		map.put("total", pagination.getTotalPages());
		map.put("records", pagination.getTotalRows());
		map.put("page", pagination.getPageIndex());
		map.put("rows", pagination.getList());
		
		return map;
	}
	
	public Pagination queryPage(Map param){
		List sqlParam = new ArrayList();
		String sql = "select * from system_role a ";
		Pagination req = super.getDaoSupport().queryListMapForPage(sql, sqlParam, Const.getPageSize(param), Const.getPageIndex(param));
		return req;
	}
	
	@Transactional
	public void save(Map param) throws Exception{
		
		String sql = "insert into demo (demo_id, demo_name) " +
				"values(?, ?)";
		String id = Const.getUUID();
		List sqlparam = new ArrayList();
		sqlparam.add(id);
		sqlparam.add(Const.getStrValue(param, "demo_name"));
		int i =super.getDaoSupport().update(sql, sqlparam);
//		super.getDaoSupport().getNamedParameterJdbcTemplate().
		log.debug(i);
		//如果需要事务回滚，使用RuntimeExcep tion 进行回滚，Exception回滚无效
//		throw new RuntimeException("aaaa");
	}
	
	public List<Map<String, Object>> loadRolePrivilegeTree(Map param){
		String role_id = Const.getStrValue(param, "role_id");
		
		String querySQl = 
				"select * from (SELECT a.menu_id id,a.parent_id pid,a.menu_name name,'menu' type,"
							+ " (select privilege_rel_id  from system_privilege_rel r where r.role_id=? and r.privilege_obj_id =a.menu_id) as privilege_rel_id "
							+ " from system_menu a order by a.sort_order ) system_menu " + 
				"union " +
				
				"SELECT b.btn_id id,b.menu_id pid,b.btn_name name,'btn' type,"
							+ " (select privilege_rel_id from system_privilege_rel r where r.role_id=? and r.privilege_obj_id =b.btn_id) as privilege_rel_id "
							+ " from system_button b ";
		List listParam = new ArrayList();
		listParam.add(role_id);
		listParam.add(role_id);
		return super.getJdbcTemplate().queryForList(querySQl,listParam.toArray());
	}
}
